
///用来管理多个球
cc.Class({
    extends: cc.Component,

    properties:()=>({

        ballPos:cc.Node,

        root:{
            default:null,
            type:cc.Node,
        },

        bar:{
            default:null,
            type:cc.Node,
        },
        ballPrefab:{
            default:null,
            type:cc.Prefab,

        },
        _gameBallList:{
            default:[],
            type:[require("BallControl")],
        },
    }),
     onLoad () {
     },

    start () {
        this._gameBallList =new Array();
      this.SpawnBall();

    },

    SpawnBall(){
        let ball= cc.instantiate(this.ballPrefab);
        ball.parent =this.root;
        ball.position =this.ballPos.position;
        let bcontrol;
        bcontrol = ball.getComponent(require("BallControl"));
        bcontrol.bar =this.bar;
        if(bcontrol===null)
        {
            cc.logError("为什么 bcontrol 为 null");
        }else {
            this._gameBallList.push(bcontrol);
        }


    },

    StartGame(){

        if(this._gameBallList.length===0){
            this.SpawnBall();
        }

        //cc.log("ballmanager spawn"+this._gameBallList.length);//1
        for(let ball in this._gameBallList){

            //cc.log(ball);
            this._gameBallList[ball].StartGame(new cc.Vec2(0,500));

        }
    },

    GameOver(){
        for(let ball in this._gameBallList){
             this._gameBallList[ball].node.destroy();
        }
        this._gameBallList.length=0;

    },

    ///全部出界为false
    CheckPos:function()
    {


        for(let ball in this._gameBallList){
            let valueY=this._gameBallList[ball].node.y;
            if(valueY+(this.root.height/2)<0){
                //cc.log("移除前的数量"+this._gameBallList.length);
                this._gameBallList[ball].node.destroy();
                this._gameBallList.splice(ball,1);//将其从表中移除
                //cc.log("移除后的数量"+this._gameBallList.length);
            }
        }

        if(this._gameBallList.length===0){
            return false;
        }else{
            return true;
        }


        ///这种是最初的方法，这样会导致的问题是，如果列表中的第一个球没有出界的话，其他的球即便是出界了也无法检测并删除，
        // if(this._gameBallList.length===0){
        //     return true;
        // }
        //
        // for(let ball in this._gameBallList){
        //     let valueY=this._gameBallList[ball].node.y;
        //     if(valueY+(this.root.height/2)<0){
        //         cc.log("移除前的数量"+this._gameBallList.length);
        //         this._gameBallList[ball].node.destroy();
        //         this._gameBallList.splice(ball,1);//将其从表中移除
        //         cc.log("移除后的数量"+this._gameBallList.length);
        //
        //         continue;
        //     }else {
        //         return true;
        //     }
        // }
        //
        // return false;

    },


    //可能出现多个球的情况。如果出现多个球，都将其变大
    AddBallBiger(time){

        for(let value of this._gameBallList){
            value.BallBigger(time);
        }

    },

    AddDoubleBall(){

        let tempList =[];
        for(let value of this._gameBallList){

            let ball= cc.instantiate(this.ballPrefab);
            ball.parent =this.root;
            ball.position =new cc.Vec2(value.node.x,value.node.y+value.node.height); //value.node.position;位置完全一样会重叠

            let bcontrol;
            bcontrol = ball.getComponent(require("BallControl"));
            bcontrol.bar =this.bar;
            if(bcontrol===null)
            {
                cc.logError("为什么 bcontrol 为 null");
            }else {
                //this._gameBallList.push(bcontrol);  //在遍历数组时再添加值是很错误的
                tempList.push(bcontrol);
            }


            let vec2 =value.GetRigidVelocity();
            if(vec2.y>0){
                bcontrol.StartGame(new cc.Vec2(-vec2.x,vec2.y));
            }else{
                bcontrol.StartGame(new cc.Vec2(vec2.x,-vec2.y));
            }
        }
        //通过将临时的列表copy出来才行
        for(let value of tempList){
            this._gameBallList.push(value);

        }
        tempList.length= 0;
    },

    GetBallNum()
    {
        return this._gameBallList.length;

    }


    // update (dt) {},
});
